<?php
require_once 'application/constant/constant.php';

/**
 * 省份列表取得
 * gcProvService
 * @author    Soochow University
 * @version   1.0
 * @exclude   作  成  日：2011/04/07
 */
class gcProvService{
    /**
     * getProvInfo
     * @return  array   $arrProv
     */
    public function getProvInfo(){
        if(!gcConfigRegistry::isRegistered("Prov")){
            $objProvInfo = new DOMDocument();
            // XML文件读取
            $objProvInfo->load(sysConstant::CONFIG_PROV_PATH);
            //省份情报取得
            $objProv = $objProvInfo->getElementById("Prov");
    
            if($objProv){
                $objElement = $objProv->getElementsByTagName("*");
                $arrProv = array();
                for($ii = 0; $ii < $objElement->length; $ii++){
                    $arrElement = array();
                    $arrElement["KEY_CODE"] = $objElement->item($ii)->getAttribute('id');
                    $arrElement["KEY_NAME"] = $objElement->item($ii)->getAttribute('name');
                    $arrProv[] = $arrElement;
                }
            } else {
                $arrProv = null;
            }
            unset($objProvInfo);
            gcConfigRegistry::setZend("Prov", $arrProv);
            gcConfigRegistry::set("Prov", $arrProv);
            return $arrProv;
        }else {
            $arrProv = gcConfigRegistry::get("Prov");
            gcConfigRegistry::setZend("Prov", $arrProv);
            return $arrProv;
        }
    }

    /**
     * getProvInfo
     * @param   mix     $arrProvCode
     * @param   string  $strKey     二维数组索引
     * @return  mix     $arrProv
     */
    public function getProvName($arrProvCode, $strKey="PROV_CODE"){
        $arrProv = $this->getProvInfo();
        if(is_array($arrProv)){
            foreach ($arrProv as $value) {
                if (is_array($arrProvCode)) {
                    foreach ($arrProvCode as &$value1) {
                        if ($value1[$strKey] == $value["KEY_CODE"]){
                            $value1[$strKey] = $value["KEY_NAME"];
                        }
                    }
                } else {
                    if ($arrProvCode == $value["KEY_CODE"]){
                        $arrProvCode = $value["KEY_NAME"];
                    }
                }
            }
        } else {
            $arrProv = null;
        }
        return $arrProvCode;
    }
}
